package com.yc.anno.junit;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class junitRunner {
    public static void main(String[] args) throws InvocationTargetException, InstantiationException, IllegalAccessException {
        junitRunner runne=new junitRunner();
        runne.runTest(MathBizTest.class);
    }
    public void runTest(Class testClass) throws InstantiationException, IllegalAccessException, InvocationTargetException {
        //创建测试对象
        Object tester=testClass.newInstance();

        List<Method> befores=new ArrayList<>();
        List<Method> afters=new ArrayList<>();
        List<Method> tests=new ArrayList<>();

        Method[] methodArray=testClass.getMethods();
        for(Method method:methodArray){
            if(method.getAnnotation(Before.class)!=null){
                befores.add(method);
            }
            if(method.getAnnotation(After.class)!=null){
                afters.add(method);
            }
            if(method.getAnnotation(Test.class)!=null){
                tests.add(method);
            }
        }
        for (Method test : tests) {
            for (Method before : befores) {
                before.invoke(tester);
            }
            try {
                test.invoke(tester);
            } catch (InvocationTargetException e) {
                //获取异常原因
                Throwable cause=e.getCause();
                System.out.println("方法业务异常 "+cause);
            }catch (AssertException e){
                System.out.println("测试结果错误");
            }
            //测试方法执行到这里，表示测试通过
            for (Method after : afters) {
                after.invoke(tester);
            }
//TODO 请统计正确的测试结果数量，异常数量，错误的数量
            System.out.println("测试结果");
        }
    }
    @Retention(RetentionPolicy.RUNTIME)
    @interface Test{
        int value();
    }
    @Retention(RetentionPolicy.RUNTIME)
    @interface Before{

    }
    @Retention(RetentionPolicy.RUNTIME)
    @interface After{

    }

}
class Assert{
    public static void assertEquals(Object aspec, Object act){
        if(!aspec.equals(act)){
            String s=String.format("期待结果是：%s,实际结果是:%s",aspec,act);
            throw new AssertException(s);
        }
    }
}
class AssertException extends RuntimeException{
   public AssertException(String msg){
       super(msg);
   }
}
